﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Crystal.CLL;
using CrystalCommon.Presenter;
using System.Configuration;

namespace CrystalCommon.View
{
    public partial class Login : BaseForm, ILoginView
    {
        #region Fields
        //public event CheckUserCrendentialsEventHandler CheckUserCrendentials;
        public event CheckUserNameEventHandler CheckUserName;
        private ILoginPresenter LoginPresenter;
        public bool IsCorrectCredentials = false;
        #endregion

        public Login()
        { 
            InitializeComponent();
            SetConnectinString();
            LoginPresenter = new LoginPresenter(this);           
        }

        #region Events
        private void SetConnectinString()
        {
            ExeConfigurationFileMap map = new ExeConfigurationFileMap();
            map.ExeConfigFilename = "App.Config";

            // now grab the configuration from the ConfigManager
            Configuration cfg = ConfigurationManager
                               .OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

            // now grab the section you're interested in from the opened config - 
            // as a sample, I'm grabbing the <appSettings> section
            AppSettingsSection section = (cfg.GetSection("appSettings") as AppSettingsSection);

            // check for null to be safe, and then get settings from the section
            if (section != null)
            {
                //string value =
                CommonCLL.mstrConnectionstring = section.Settings["SqlServer"].Value;                
            }

            ConnectionStringsSection connectionContext = (cfg.GetSection("connectionStrings") as ConnectionStringsSection);
            if (connectionContext != null)
            {
                CommonCLL.mstrContextConnectionstring = connectionContext.ConnectionStrings["CrystalEntities"].ConnectionString ;
            }
        }

        private void Login_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                UnloadEvents();
            }
            catch (Exception ex)
            {
                this.LogErrorToDatabase(ex);
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (CheckUserName(txtUserName.Text.Trim(), txtPassword.Text.Trim()))
            {
                //CommonCLL.UserID
                IsCorrectCredentials = true;
                this.Close();
            }
            else
            {
                IsCorrectCredentials = false;
                ShowMessageBox(DBResult.LOGIN_FAILED, BaseClass.Capitalize("Login"));
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        #endregion

        #region Methods
        private void UnloadEvents()
        {
            //this.CheckUserCrendentials -= this.LoginPresenter.view_CheckUserCredentials;
        }
        #endregion

        private void Login_Load(object sender, EventArgs e)
        {
            //txtUserName.Text = "admin";
            //txtPassword.Text = "admin";
            //btnLogin.PerformClick();
        }

    }
}
